/*
 * Created by JFormDesigner on Thu Jun 27 11:04:12 CST 2024
 */

package scct.cuixinlong.ui;

import scct.cuixinlong.dao.UserDao;

import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.*;

/**
 * @author Lenovo
 */
public class DlgUserAdd extends JDialog {
    public DlgUserAdd(Window owner) {
        super(owner);
        initComponents();
    }

    private void btnAdd(ActionEvent e) {
        // TODO add your code here
        String username = txtName.getText();
        String pass = txtPass.getText();
        int age = Integer.parseInt(txtAge.getModel().getValue().toString());
        String gender = radMale.isSelected() ? "男" : "女";
        String mobile = txtMobile.getText();
        if (!username.matches("^\\w{3,8}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的用户名");
            return;
        }
        if (!pass.matches("^\\w{3,8}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的密码");
            return;
        }
        if (age < 10 || age > 100) {
            JOptionPane.showMessageDialog(this, "请输入正确的年龄");
            return;
        }
        if (!mobile.matches("^1[3-9]\\d{9}$")) {
            JOptionPane.showMessageDialog(this, "请输入合法的手机号码");
            return;
        }
        UserDao userDao = new UserDao();
        if (userDao.isDupname(username)) {
            JOptionPane.showMessageDialog(this, "你输入的用户名已经存在,请重新输入");
            return;
        }


        if (userDao.addUser(username, pass, gender, age, mobile)) {
            JOptionPane.showMessageDialog(this, "添加成功");
        }
        this.dispose();
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - cuixinlong
        label2 = new JLabel();
        label3 = new JLabel();
        label1 = new JLabel();
        label8 = new JLabel();
        label5 = new JLabel();
        radMale = new JRadioButton();
        radFemale = new JRadioButton();
        btnCancel = new JButton();
        btnAdd = new JButton();
        label4 = new JLabel();
        txtAge = new JSpinner();
        txtName = new JTextField();
        txtPass = new JTextField();
        txtMobile = new JTextField();
        this2 = new JDialog();
        label7 = new JLabel();
        label9 = new JLabel();
        label10 = new JLabel();
        label11 = new JLabel();
        label12 = new JLabel();
        label13 = new JLabel();
        radioButton3 = new JRadioButton();
        radioButton4 = new JRadioButton();
        button4 = new JButton();
        button5 = new JButton();
        label14 = new JLabel();
        spinner2 = new JSpinner();
        txtName3 = new JTextField();
        txtPass3 = new JTextField();
        txtMobile3 = new JTextField();

        //======== this ========
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label2 ----
        label2.setText("\u5bc6\u7801");
        label2.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label2);
        label2.setBounds(185, 135, 45, 25);

        //---- label3 ----
        label3.setText("\u624b\u673a\u53f7\u7801");
        label3.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label3);
        label3.setBounds(170, 175, 70, 25);

        //---- label1 ----
        label1.setText("\u7528\u6237\u540d");
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label1);
        label1.setBounds(190, 85, 45, 25);

        //---- label8 ----
        label8.setText("\u6027\u522b");
        contentPane.add(label8);
        label8.setBounds(150, 180, 45, 0);

        //---- label5 ----
        label5.setText("\u6027\u522b");
        label5.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label5);
        label5.setBounds(190, 260, 45, 25);

        //---- radMale ----
        radMale.setText("\u7537");
        contentPane.add(radMale);
        radMale.setBounds(new Rectangle(new Point(280, 260), radMale.getPreferredSize()));

        //---- radFemale ----
        radFemale.setText("\u5973");
        contentPane.add(radFemale);
        radFemale.setBounds(410, 260, 50, 25);

        //---- btnCancel ----
        btnCancel.setText("\u53d6\u6d88");
        contentPane.add(btnCancel);
        btnCancel.setBounds(385, 305, 78, 34);

        //---- btnAdd ----
        btnAdd.setText("\u6dfb\u52a0");
        btnAdd.addActionListener(e -> btnAdd(e));
        contentPane.add(btnAdd);
        btnAdd.setBounds(new Rectangle(new Point(165, 305), btnAdd.getPreferredSize()));

        //---- label4 ----
        label4.setText("\u5e74\u9f84");
        label4.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label4);
        label4.setBounds(190, 210, 45, 25);
        contentPane.add(txtAge);
        txtAge.setBounds(255, 210, 255, txtAge.getPreferredSize().height);
        contentPane.add(txtName);
        txtName.setBounds(255, 90, 255, txtName.getPreferredSize().height);
        contentPane.add(txtPass);
        txtPass.setBounds(255, 125, 255, 34);
        contentPane.add(txtMobile);
        txtMobile.setBounds(255, 160, 255, 34);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(590, 395);
        setLocationRelativeTo(getOwner());

        //======== this2 ========
        {
            var this2ContentPane = this2.getContentPane();
            this2ContentPane.setLayout(null);

            //---- label7 ----
            label7.setText("\u7528\u6237\u540d");
            this2ContentPane.add(label7);
            label7.setBounds(75, 70, 0, 35);

            //---- label9 ----
            label9.setText("\u5bc6\u7801");
            label9.setHorizontalAlignment(SwingConstants.CENTER);
            this2ContentPane.add(label9);
            label9.setBounds(170, 135, 45, 25);

            //---- label10 ----
            label10.setText("\u624b\u673a\u53f7\u7801");
            label10.setHorizontalAlignment(SwingConstants.CENTER);
            this2ContentPane.add(label10);
            label10.setBounds(157, 175, 70, 25);

            //---- label11 ----
            label11.setText("\u7528\u6237\u540d");
            label11.setHorizontalAlignment(SwingConstants.CENTER);
            this2ContentPane.add(label11);
            label11.setBounds(170, 85, 45, 25);

            //---- label12 ----
            label12.setText("\u6027\u522b");
            this2ContentPane.add(label12);
            label12.setBounds(150, 180, 45, 0);

            //---- label13 ----
            label13.setText("\u6027\u522b");
            label13.setHorizontalAlignment(SwingConstants.CENTER);
            this2ContentPane.add(label13);
            label13.setBounds(170, 245, 45, 25);

            //---- radioButton3 ----
            radioButton3.setText("\u7537");
            this2ContentPane.add(radioButton3);
            radioButton3.setBounds(new Rectangle(new Point(250, 245), radioButton3.getPreferredSize()));

            //---- radioButton4 ----
            radioButton4.setText("\u5973");
            this2ContentPane.add(radioButton4);
            radioButton4.setBounds(350, 250, 50, 25);

            //---- button4 ----
            button4.setText("\u53d6\u6d88");
            this2ContentPane.add(button4);
            button4.setBounds(385, 310, 78, 34);

            //---- button5 ----
            button5.setText("\u6dfb\u52a0");
            this2ContentPane.add(button5);
            button5.setBounds(new Rectangle(new Point(165, 305), button5.getPreferredSize()));

            //---- label14 ----
            label14.setText("\u5e74\u9f84");
            label14.setHorizontalAlignment(SwingConstants.CENTER);
            this2ContentPane.add(label14);
            label14.setBounds(170, 215, 45, 25);
            this2ContentPane.add(spinner2);
            spinner2.setBounds(265, 210, 225, spinner2.getPreferredSize().height);
            this2ContentPane.add(txtName3);
            txtName3.setBounds(250, 80, 255, txtName3.getPreferredSize().height);
            this2ContentPane.add(txtPass3);
            txtPass3.setBounds(235, 125, 255, 34);
            this2ContentPane.add(txtMobile3);
            txtMobile3.setBounds(250, 165, 255, 34);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < this2ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = this2ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = this2ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                this2ContentPane.setMinimumSize(preferredSize);
                this2ContentPane.setPreferredSize(preferredSize);
            }
            this2.pack();
            this2.setLocationRelativeTo(this2.getOwner());
        }

        //---- buttonGroup1 ----
        var buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radMale);
        buttonGroup1.add(radFemale);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - cuixinlong
    private JLabel label2;
    private JLabel label3;
    private JLabel label1;
    private JLabel label8;
    private JLabel label5;
    private JRadioButton radMale;
    private JRadioButton radFemale;
    private JButton btnCancel;
    private JButton btnAdd;
    private JLabel label4;
    private JSpinner txtAge;
    private JTextField txtName;
    private JTextField txtPass;
    private JTextField txtMobile;
    private JDialog this2;
    private JLabel label7;
    private JLabel label9;
    private JLabel label10;
    private JLabel label11;
    private JLabel label12;
    private JLabel label13;
    private JRadioButton radioButton3;
    private JRadioButton radioButton4;
    private JButton button4;
    private JButton button5;
    private JLabel label14;
    private JSpinner spinner2;
    private JTextField txtName3;
    private JTextField txtPass3;
    private JTextField txtMobile3;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
